//
// Created by tang on 22-11-4.
//

#include "drag_helper.hpp"

DragHelper::DragHelper(GLFWwindow *win): win_(win), active_(false), primary_(false), last_position_x_(0), last_position_y_(0) {

}

void DragHelper::InjectPress(bool primary) {
    active_ = true;
    primary_ = primary;
    glfwGetCursorPos(win_, &last_position_x_, &last_position_y_);
}

void DragHelper::InjectRelease() {
    active_ = false;
}

void DragHelper::InjectPosition(double x, double y) {
    if (!active_)
        return;

    auto const dx = x - last_position_x_;
    auto const dy = y - last_position_y_;
    if (auto & act = actions_[(int)!primary_])
        act(dx, dy);
    last_position_x_ = x;
    last_position_y_ = y;
}

void DragHelper::SetActions(DragAction primary, DragAction secondly) {
    actions_[0] = std::move(primary);
    actions_[1] = std::move(secondly);
}
